<?php
use adminmatrix\migration\Migrator;
use  adminmatrix\migration\db\Column;
use Phinx\Db\Adapter\AdapterInterface;

class AdminSystemSetting extends Migrator
{
    protected array $menuItems = [
        ['id' => 30, 'path' => 'setting', 'name' => '系统设置', 'parent_id' => 0, 'component' => '', 'type' => 1, 'perms' => '','app' => 'adminapi', 'icon' => 'ant-icon-SettingOutlined', 'hidden' => 0],
        ['id' => 31, 'path' => 'configs', 'name' => '设置', 'parent_id' => 30, 'component' => 'system/setting/system/index', 'type' => 2, 'perms' => '/adminapi/system.config/lists', 'icon' => 'ant-icon-SettingOutlined', 'hidden' => 0],
        ['id' => 32, 'path' => '', 'name' => '设置权限', 'parent_id' => 12, 'component' => '', 'type' => 3, 'perms' => '/adminapi/system.role/setAuth','app' => 'adminapi', 'icon' => '', 'hidden' => 0],
        ['id' => 33, 'path' => '', 'name' => '新增配置', 'parent_id' => 31, 'component' => '', 'type' => 3, 'perms' => '/adminapi/system.config/save','app' => 'adminapi', 'icon' => '', 'hidden' => 0],
        ['id' => 34, 'path' => '', 'name' => '更新配置', 'parent_id' => 31, 'component' => '', 'type' => 3, 'perms' => '/adminapi/system.config/update','app' => 'adminapi', 'icon' => '', 'hidden' => 0],
        ['id' => 35, 'path' => '', 'name' => '删除配置', 'parent_id' => 31, 'component' => '', 'type' => 3, 'perms' => '/adminapi/system.config/delete','app' => 'adminapi', 'icon' => '', 'hidden' => 0],
        ['id' => 36, 'path' => '', 'name' => '新增tag', 'parent_id' => 31, 'component' => '', 'type' => 3, 'perms' => '/adminapi/system.config/addGroup','app' => 'adminapi', 'icon' => '', 'hidden' => 0],
        ['id' => 37, 'path' => 'database', 'name' => '数据库', 'parent_id' => 30, 'component' => null, 'type' => 1, 'perms' => '','app' => 'adminapi', 'icon' => 'ant-icon-DatabaseFilled', 'hidden' => 0, 'select' => '', 'create_time' => 1747722998, 'update_time' => 1747722998, 'delete_time' => null],
        ['id' => 38, 'path' => 'mysql', 'name' => 'mysql表操作', 'parent_id' => 37, 'component' => 'system/setting/database/mysql/index', 'type' => 2, 'perms' => '1','app' => 'adminapi', 'icon' => 'ant-icon-ConsoleSqlOutlined', 'hidden' => 0, 'select' => '', 'create_time' => 1747723052, 'update_time' => 1747723052, 'delete_time' => null],
        ['id' => 39, 'path' => '', 'name' => '字段更新', 'parent_id' => 38, 'component' => null, 'type' => 3, 'perms' => '','app' => 'adminapi', 'icon' => '', 'hidden' => 0, 'select' => '', 'create_time' => 1747723440, 'update_time' => 1747723440, 'delete_time' => null],
        ['id' => 40, 'path' => '', 'name' => '字段删除', 'parent_id' => 38, 'component' => '', 'type' => 3, 'perms' => '','app' => 'adminapi', 'icon' => '', 'hidden' => 0, 'select' => '', 'create_time' => 1747723450, 'update_time' => 1747723450, 'delete_time' => null],
        ['id' => 41, 'path' => 'migrations', 'name' => '数据库迁移', 'parent_id' => 37, 'component' => 'system/setting/database/migrations/index', 'type' => 2, 'perms' => '','app' => 'adminapi', 'icon' => 'ant-icon-AlertFilled', 'hidden' => 0, 'select' => '', 'create_time' => 1747723529, 'update_time' => 1747723529, 'delete_time' => null],
        ['id' => 42, 'path' => '', 'name' => '执行迁移', 'parent_id' => 41, 'component' => null, 'type' => 3, 'perms' => '','app' => 'adminapi', 'icon' => '', 'hidden' => 0, 'select' => '', 'create_time' => 1747723577, 'update_time' => 1747723577, 'delete_time' => null],
        ['id' => 43, 'path' => '', 'name' => '数据回滚', 'parent_id' => 41, 'component' => '', 'type' => 3, 'perms' => '','app' => 'adminapi', 'icon' => '', 'hidden' => 0, 'select' => '', 'create_time' => 1747723587, 'update_time' => 1747723587, 'delete_time' => null],
        ['id' => 44, 'path' => 'notification', 'name' => '消息设置', 'parent_id' => 30, 'component' => 'system/setting/notification/setting', 'type' => 2, 'perms' => '','app' => 'adminapi', 'icon' => 'ant-icon-NotificationFilled', 'hidden' => 0, 'select' => '', 'create_time' => 1747723642, 'update_time' => 1747723642, 'delete_time' => null]
    ];
    /**
    * Change Method.
    *
    * Write your reversible migrations using this method.
    *
    * More information on writing migrations is available here:
    * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
    *
    * The following commands can be used in this method and Phinx will
    * automatically reverse them when rolling back:
    *
    *    createTable
    *    renameTable
    *    addColumn
    *    renameColumn
    *    addIndex
    *    addForeignKey
    *
    * Remember to call "create()" or "update()" and NOT "save()" when working
    * with the Table class.
    */
   public function up()
   {
       // 记录开始时间
       $startTime = microtime(true);

       $this->output->info("== 执行创建系统配置tag表");
       $this->createAdminSystem();

       $this->output->info("==执行创建系统配置参数表");
       $this->createAdminSystemConfig();

       $this->output->info("== 执行创建菜单插入");
       $this->saveMenu();

       // 记录结束时间
       $endTime = microtime(true);

       // 计算执行时间（秒）
       $executionTime = $endTime - $startTime;
       $executionTime = number_format($executionTime, 6, '.', '');

       $this->output->write("== 创建系统配置表成功 总计用时：{$executionTime}秒" , true);
   }



   public function down(){
       $piex = env("DB_PREFIX");
       // 记录开始时间
       $startTime = microtime(true);

       // 记录结束时间
       $endTime = microtime(true);

       // 计算执行时间（秒）
       $executionTime = $endTime - $startTime;
       $executionTime = number_format($executionTime, 6, '.', '');

       $this->output->info("== 删除系统配置表 总计用时：{$executionTime}秒");
   }

    private function saveMenu(){


        if ($this->hasTable('admin_permission') && $this->isMigratingUp())
        {
            $this->table('admin_permission')->insert($this->menuItems)->save();
            $this->output->info("== 创建菜单完成用");
        }

    }

   private function createAdminSystem()
   {
       if ($this->hasTable('admin_system')) {
           return $this->output->error("== 系统配置tag表已存在 === 跳过");
       }

       $startTime = microtime(true);

       $table = $this->table('admin_system', [
           'engine' => 'InnoDB',
           'collation' => 'utf8mb4_general_ci',
           'id' => 'id',
           'primary_key' => ['id'],
           'comment' => '系统配置tag表',
       ]);

       $table->addColumn('title', AdapterInterface::PHINX_TYPE_CHAR, [
           'limit' => \Phinx\Db\Adapter\MysqlAdapter::INT_TINY,
           'comment' => 'tag标题'
       ]);

       $table->create();

       // 记录结束时间
       $endTime = microtime(true);

       // 计算执行时间（秒）
       $executionTime = $endTime - $startTime;
       $executionTime = number_format($executionTime, 6, '.', '');

       $this->output->info("== 创建 系统配置参数表 完成 总计用时：{$executionTime}秒");

       if ($this->hasTable('admin_system') && $this->isMigratingUp()) {
            $this->table('admin_system') ->insert([
                [
                    'title' =>  '系统配置'
                ]
            ])->save();
       }
   }

    /**
     * 系统配置参数表
     * @return void
     */
   private function createAdminSystemConfig()
   {
       if($this->hasTable('admin_system_config'))
       {
           return $this->output->error("== 系统配置参数表已存在 === 跳过");
       }

       $startTime = microtime(true);


       $table = $this->table("admin_system_config" , [
           'engine' => 'InnoDB',
           'collation' => 'utf8mb4_general_ci',
           'id'=>'id',
           'primary_key'    =>  ['id'],
           'comment' => '系统配置参数表',
       ]);

       $table->addColumn("title" , AdapterInterface::PHINX_TYPE_STRING, [
           'limit' => \Phinx\Db\Adapter\MysqlAdapter::BLOB_SMALL,
           'comment'        =>  '参数标题',
           'null'           =>  true
       ]);

       $table->addColumn('name' , AdapterInterface::PHINX_TYPE_CHAR , [
           'limit'  =>  \Phinx\Db\Adapter\MysqlAdapter::BIT,
           "null"   =>  true,
           'comment'    =>  '参数唯一标识'
       ]);

       $table->addColumn('group_id' , AdapterInterface::PHINX_TYPE_INTEGER , [
           'signed'     =>  false,
           'comment'    =>  '配置id'
       ]);

       $table->addColumn("status" , AdapterInterface::PHINX_TYPE_TINY_INTEGER , [
           'limit'  =>  1,
           'null'   =>  false,
           'default'    =>  1,
           'comment'    =>  '是否开启 1:开启|0:关闭'
       ]);

       $table->addColumn('input' , AdapterInterface::PHINX_TYPE_CHAR , [
           'limit'  =>  \Phinx\Db\Adapter\MysqlAdapter::INT_TINY,
           'null'   =>  true,
           'default'    =>  'input',
           'comment'    =>  '字段类型'
       ]);

       $table->addColumn('value' , AdapterInterface::PHINX_TYPE_TEXT , [
           'null'   =>  false,
           'comment'    =>  '字段值'
       ]);

       $table->addColumn('create_time' , AdapterInterface::PHINX_TYPE_INTEGER , [
           'limit'  =>  \Phinx\Db\Adapter\MysqlAdapter::INT_DISPLAY_REGULAR,
           'comment'    =>  '创建时间'
       ]);

       $table->addColumn('update_time' , AdapterInterface::PHINX_TYPE_INTEGER , [
           'limit'  =>  \Phinx\Db\Adapter\MysqlAdapter::INT_DISPLAY_REGULAR,
           'comment'    =>  '编辑时间'
       ]);

       $table->addColumn('delete_time' , AdapterInterface::PHINX_TYPE_INTEGER , [
           'limit'  =>  \Phinx\Db\Adapter\MysqlAdapter::INT_DISPLAY_REGULAR,
           'comment'    =>  '删除时间'
       ]);

       $table->addIndex(["name"]);

       $table->create();


       // 记录结束时间
       $endTime = microtime(true);

       // 计算执行时间（秒）
       $executionTime = $endTime - $startTime;
       $executionTime = number_format($executionTime, 6, '.', '');

       $this->output->info("== 创建 系统配置参数表 完成 总计用时：{$executionTime}秒");

       if ($this->hasTable('admin_system_config') && $this->isMigratingUp()) {
           $this->table('admin_system_config')->insert([
               [
                    'title' =>  '系统名',
                    'name'  =>  'system_web_name',
                   'group_id'   =>  1,
                   'status'     =>  1,
                   'input'      =>  'input',
                   'value'      =>  '后台管理系统',
               ]
           ])->save();
       }

   }
}